Всем привет) Есть меню сайта. При прокрутке страницы должны выделяться активные пункты меню. Следующий код работает, но есть один баг: когда нажимаешь на пункты меню и происходит переход на соответствующую страницу, то все работает нормально, но стоит перейти на главную страницу прокруткой (мышкой или через полосу прокрутки), как подсветка пункта меню главной страницы исчезает. И если снова на этот пункт нажать, то он опять будет подсвечиваться. Причем это происходит только с главной (самой первой) страницей. Может кто сможет решить проблему? Заранее спасибо!
// Cache selectors
var lastId,
topMenu = $("#menu"),
topMenuHeight = topMenu.outerHeight() + 15,
// All list items
menuItems = topMenu.find("a"),
// Anchors corresponding to menu items
scrollItems = menuItems.map(function() {
var item = $($(this).attr("href"));
if (item.length) {
return item;
}
});
// Bind click handler to menu items
// so we can get a fancy scroll animation
menuItems.click(function(e) {
var href = $(this).attr("href"),
offsetTop = href === "#" ? 0 : $(href).offset().top - topMenuHeight + 1;
$('html, body').stop().animate({
scrollTop: offsetTop
}, 300);
e.preventDefault();
});
// Bind to scroll
$(window).scroll(function() {
// Get container scroll position
var fromTop = $(this).scrollTop() + topMenuHeight;
// Get id of current scroll item
var cur = scrollItems.map(function() {
if ($(this).offset().top < fromTop)
return this;
});
// Get the id of the current element
cur = cur[cur.length - 1];
var id = cur && cur.length ? cur[0].id : "";
if (lastId !== id) {
lastId = id;
// Set/remove active class
menuItems
.parent().removeClass("active")
.end().filter("[href='#" + id + "']").parent().addClass("active");
}
});
.container {
width: 1100px;
}
#logo {
margin-top: -7px;
height: 35px;
width: 100px;
}
.navbar a {
margin-top: 20px;
}
.navbar {
background-color: rgba(255, 255, 255, 0);
border: rgba(255, 255, 255, 0);
}
.navbar-inverse .navbar-nav > .active > a,
.navbar-inverse .navbar-nav > .active > a:hover,
.navbar-inverse .navbar-nav > .active > a:focus {
background-color: rgba(255, 255, 255, 0);
}
.navbar-inverse .navbar-nav > .active {
background-color: #7fc400;
border-radius: 0 0 5px 5px;
}
<header>
<nav class="navbar navbar-inverse navbar-fixed-top" role="navigation">
<div class="container">
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav navbar-right" id="menu">
<li class="active"><a href="#home">ГЛАВНАЯ</a></li>
<li><a href="#features">ПРЕИМУЩЕСТВА</a></li>
<li><a href="#catalog">КАТАЛОГ</a></li>
</ul>
</div>
</div>
</nav>
</header>
<!-- Home -->
<section id="home">
<img src="http://karriarstudenten.se/wp-content/uploads/2016/03/nature-sky-sunset-man.jpeg">
</section>
<!-- Features -->
<section id="features">
<img src="http://karriarstudenten.se/wp-content/uploads/2016/03/nature-sky-sunset-man.jpeg">
</section>
<!-- Catalog -->
<section id="catalog">
<img src="http://karriarstudenten.se/wp-content/uploads/2016/03/nature-sky-sunset-man.jpeg">
</section>